local AStar = require("scripts.pubs.AStar");
local MapMgr = class("MapMgr");
function MapMgr:ctor()
    self.mapId = 0;
    self.map = nil;
    self.mapData = {};
end

function MapMgr:setMapId(id)
    self.mapId = id;
    self.map = cc.TMXTiledMap:create(mgrs.res:getMapPath(id));
    local mapSize = self:getMapSize();--多少个瓦片
    local titleSize = self:getTileSize();
    local wallLayer = self:getLayer("wall");
    for j = 1,mapSize.height do
        local tmp = {};
        for i = 1,mapSize.width do
            local x = wallLayer:getTileGIDAt(cc.p(i-1,j-1));
            table.insert(tmp,x);
        end
        table.insert(self.mapData,tmp);
    end
    self.aStar = AStar.new(self.mapData);
end

function MapMgr:resetMap(mapId)
    self.mapData = {};
    self:resetMap(mapId);
end

function MapMgr:getMap()
    return self.map;
end
--多少个瓦片
function MapMgr:getMapSize()
    return self.map:getMapSize();
end
--每一个瓦片的宽度
function MapMgr:getTileSize()
    return self.map:getTileSize();
end
function MapMgr:contentSize()
    return self.map:getContentSize();
end

function MapMgr:getLayer(name)
    return self.map:getLayer(name);  
end

function MapMgr:getObjectGroup(name)
    return self.map:getObjectGroup(name);
end

--转成格子坐标
function MapMgr:tiledCoorForPosition(position)
    local mapSize = self:getMapSize();
    local tileSize = self:getTileSize();
    local x = position.x / tileSize.width;
    local y = (mapSize.height * tileSize.height - position.y) / tileSize.height;
    x = ( math.ceil(x) == x )  and x or math.ceil(x) - 1;
    y = ( math.ceil(y) == y )  and y or math.ceil(y) - 1;
    return cc.p(x,y);
end

--转成oepngl坐标
function MapMgr:positionForTiledCoor(tiledCoor)
    local mapSize = self:getMapSize();
    local tileSize = self:getTileSize();
    local x = tiledCoor.x * tileSize.width + tileSize.width / 2;
    local y = (mapSize.height * tileSize.height) - (tiledCoor.y * tileSize.height + tileSize.height / 2);
    return cc.p(x,y);
end

function MapMgr:callWalk(point)
    point = self:tiledCoorForPosition(point);
    local x = point.y + 1;
    local y = point.x + 1;
    return self.mapData[x][y] == 0;
end

function MapMgr:findWay(startPoint,endPoint)
    local start = self:tiledCoorForPosition(startPoint);
    local endx = self:tiledCoorForPosition(endPoint);
    print(start.y,start.x,endx.y,endx.x)
    local way = self.aStar:searchPatch(start.y,start.x,endx.y,endx.x);
    local path = {};
    if way ~= 0 and way ~= -1 then
        for key, var in pairs(way) do
            table.insert(path,self:positionForTiledCoor(cc.p(var.y,var.x)));
        end
        return path;
    end
    return way;
end

function MapMgr:setViewPort(point)
    local x = point.x;
    local y = point.y;
    local size = self:contentSize();
    if x > size.width or y > size.height or x < 0 or y < 0 then
        return;
    end
    local viewPoint = cc.p(0,0);
    local winSize = self:winSize();

    viewPoint.x = x - winSize.width/2;
    viewPoint.y = y - winSize.height/2;
    viewPoint.x = math.max(viewPoint.x,0);
    viewPoint.y = math.max(viewPoint.y,0);
    viewPoint.x = math.min(viewPoint.x,size.width - winSize.width);
    viewPoint.y = math.min(viewPoint.y,size.height - winSize.height);
    self.map:getParent():setPosition(cc.p(-viewPoint.x,-viewPoint.y));
end

function MapMgr:winSize()
    return display.size;
end

function MapMgr:computePath()
    
end


mgrs.map = nil;
mgrs.singleClass(mgrs,MapMgr,"map");

--endregion
